Skip to main content

Operators

Python possesses a number of operators, or symbols, that are used to perform various operations on objects. Many of them are very important for efficient Python programming, so in this section we’ll go over the ones that everyone should be familiar with. Don’t worry about memorizing all of them. Use this as an opportunity to see what exists and use this page as a reference to remind yourself!

Arithmetic Operators

Arithmetic operators perform the typical mathematical operations you may be familiar with when used with integer and float. Certain operators can also perform operations with certain other types. This functionality can be very useful, and we’ll discuss which operators work on which object type in their respective segments.

Arithmetic OperatorDescriptionExample
a + bAdds a and b together1 + 2 = 3
a - bSubtracts b from a1 - 2 = -1
a * bMultiples a and b together1 * 2 = 2
a / bDivides a by b1 / 2 = 0.5
a % bPerforms a modulo division of a by b7 % 3 = 1
a ** bRaises a to the power of b2 ** 3 = 8
a // bPerforms a floor division of a by b1 // 2 = 0

Logical Operators

Logical operators perform logical operations on values and return a boolean value (True or False). They're fundamental in decision making, helping you create code that operates based on whether certain conditions are met.

Logical OperatorDescriptionExample
A and BReturns True if both given statements are True. Otherwise, it returns False.x = 5
print(x > 4 and x < 8)
#Outputs True
A or BReturns True if at least one of the given statements is True.x = 5
print(x > 4 or x < 8)
#Outputs True
A not BReverses the statement.x = 5
print(x > 4 not x < 8)
#Outputs False
x=1
print(not(x>1 and x<8))
#Outputs True

Comparison Operators

Comparison operators perform comparison operations on values and return a boolean value (True or False). They help you create codes that operate based on the relationship between objects.

Comparison OperatorDescriptionExample
A == BReturns True if A equals B. Otherwise, it returns False.x = 5
print(x == 5) # Outputs True
print(x == 4) # Outputs False
A != BReturns True if A does not equal B. Otherwise, it returns False.x = 5
print(x != 5) # Outputs False
print(x != 4) # Outputs True
A > BReturns True if A is greater than B. Otherwise, it returns False.x = 5
print(x > 3) # Outputs True
print(x > 6) # Outputs False
A < BReturns True if A is less than B. Otherwise, it returns False.x = 5
print(x < 3) # Outputs False
print(x < 6) # Outputs True
A >= BReturns True if A is greater than or equal to B. Otherwise, it returns False.x = 5
print(x >= 5) # Outputs True
print(x >= 6) # Outputs False
A <= BReturns True if A is less than or equal to B. Otherwise, it returns False.x = 5
print(x <= 5) # Outputs True
print(x <= 6) # Outputs True

Identity Operators

Identity operators perform identity operations on values and return a boolean value (True or False).

Identity OperatorsDescriptionExample
A is BReturns True if A is the same exact object as B. Otherwise, it returns False.x = 5
print(x is 5) # Outputs True
A is not BReturns True if A is not the same object as B. Otherwise, it returns False.x = 5
print(x is not 5) # Outputs True

It’s important to note that the == operator and is operator are not the same. The == operator compares the values of two objects, while the is operator compares the identities of the two objects. Here’s a good example-

list1=[1,2,3]
list2=[1,2,3]
print(list1 is list2)#Outputs False
print(list1==list2) #Outputs True

list1==list2 outputs True because both lists contain the same values, while list1 is list2 outputs False because the two lists are two distinct objects, which we know because they’re assigned to different variables.

Membership Operators

Membership operators perform membership operations on values and return a boolean value (True or False). They’re very important when working with data structures such as lists and strings, allowing for concise and readable code that checks whether certain objects are in those data structures.

Membership OperatorDescriptionExample
A in BReturns True if A is inside sequence type object B. Otherwise, it returns False.x = 1
list1 = [1, 2, 3]
print(1 in list1)
# Outputs True
A not in BReturns True if A is not inside sequence type object B. Otherwise, it returns False.x = 5
list1 = [1, 2, 3]
print(5 not in list1)
# Outputs True

Conclusion

Whew, that's a lot of operators! This may seem overwhelming right now, but don't worry! As you start working through examples and applying these operators in different scenarios, their uses and nuances will become much clearer. Think of each operator as a tool in your programming toolkit, each with its own purpose and best use case.